﻿

using Admin.NET.Core;
using FluentEmail.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using IotContract.Models.Clients;
using IotContract.Models.Process;
using IotFun.Const;
using IotFun.Entity;
using IotFun.Services;
using IotFun.Views.EquipInfoView.Dto;
using IotFun.Views.EquipValueView.Dto;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IotFun.Views.EquipValueView;
/// <summary>
/// 设备实时数据服务
/// </summary>
[ApiDescriptionSettings(IotConst.GroupName)]
public class EquipValueService:IDynamicApiController, ITransient
{
    private SqlSugarRepository<EquipInfo> _rep;
    private GrpcService _grpcService;
    public EquipValueService(SqlSugarRepository<EquipInfo> rep,GrpcService grpcService)
    {
        _rep = rep;
        _grpcService = grpcService;
    }
    /// <summary>
    /// 分页查询设备管理
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<EquipValueOutput>> Page([FromQuery] EquipInfoInput input)
    {
        var query = _rep.AsQueryable().Where(x=>x.IsEnable==1)
                    .WhereIF(!string.IsNullOrWhiteSpace(input.equiptype), u => u.EquipType == input.equiptype)
                     .WhereIF(!string.IsNullOrWhiteSpace(input.protobuftype), u => u.ProtobufType == input.protobuftype)
                    .LeftJoin<EquipGroup>((x, y) => x.GroupId == y.Id).LeftJoin<ProgramInfo>((x, y, z) => x.ProgramId == z.Id).
                    WhereIF(input.programid > 0, (x, y, z) => z.Id == input.programid)
                    .WhereIF(!string.IsNullOrWhiteSpace(input.name), (x, y, z) => x.Name.Contains(input.name.Trim()) || x.IPAddress==input.name.Trim() || x.Num.Contains(input.name.Trim()) || y.Name.Contains(input.name.Trim()) || x.UnitNum == input.name.Trim() || x.BindNum == input.name.Trim())
                    .Where(x => x.IsDelete == false)
                    .Select((x, y, z) => new EquipValueOutput
                    {
                        Id = x.Id,    
                        ProgramId=x.ProgramId,
                        ProgramName=z.Name,
                        Name = x.Name,
                        UnitNum= x.UnitNum,
                        EquipType = x.EquipType,
                        ProtobufType= x.ProtobufType

                    }, true);
        ;

        var result= await query.ToPagedListAsync(input.Page, input.PageSize);

        if (result.Items.Count() == 0) return result;
       var commands= result.Items.Select(x => new ClientRequestCommand
       {
           EquipId=x.Id,
           ProtobufType=x.ProtobufType
       });

        // 关联设备数据
       var equips= await _grpcService.GetEquips(commands.ToList());

        if (equips == null)
        {
            return result;
        }
        result.Items.ForEach(item =>
        {
            var equip = equips.FirstOrDefault(x => x.Id == item.Id);
            if(equip != null)
            {
                item.ReadTime = equip.ReadTime;
                item.Value = equip.Value;
                item.HasTags = equip.HasTags;
                item.Status = equip.Status;
                item.Connection = equip.Connection;
                item.Remark = equip.Remark;
            }
        });

        return result;

    }
  
}
